package com.hsd.framework.util;

import lombok.extern.slf4j.Slf4j;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;

@Slf4j
public class BeetlUtils {
    /**
     * 根据模板生产文件
     *
     * @param context
     * @param saveFileName
     */
    public static void renderToFile(String readTlFileName, Map context, String saveFileName) {
        try {
            Configuration cfg = Configuration.defaultConfiguration();
            cfg.setHtmlTagFlag(YamlConfigurerUtil.getStrYmlVal("beetl.html_tag_flag"));
            cfg.setPlaceholderStart(YamlConfigurerUtil.getStrYmlVal("beetl.delimiter_placeholder_start"));
            cfg.setPlaceholderEnd(YamlConfigurerUtil.getStrYmlVal("beetl.delimiter_placeholder_end"));
            cfg.setStatementStart(YamlConfigurerUtil.getStrYmlVal("beetl.delimiter_statement_start"));
            cfg.setStatementEnd(YamlConfigurerUtil.getStrYmlVal("beetl.delimiter_statement_end"));
            cfg.addPkg(YamlConfigurerUtil.getStrYmlVal("beetl.cfg_package"));
            GroupTemplate gt = new GroupTemplate(new ClasspathResourceLoader(), cfg);
            gt.registerFunctionPackage("strUtil", new StrUtil());
            gt.registerFunctionPackage("validator", new ValidatorUtil());
            Template template = gt.getTemplate(readTlFileName);
//			String output=template.render();
            template.binding(context);

            File dir = new File(saveFileName.substring(0, saveFileName.lastIndexOf("/")));
            if (!dir.isDirectory()) {
                dir.mkdirs();
            }
            // 输出到文件
            FileOutputStream of = new FileOutputStream(saveFileName);
            of.write(template.render().getBytes(StandardCharsets.UTF_8));
            of.flush();
            of.close();
            log.info("==success==" + saveFileName);
        } catch (Exception e) {
            log.error("==error==" + saveFileName, e);
        }
    }

    /**
     * 根据模板生产字符串
     */
    public static String renderToString(String readTlFileName, Map context) {
        try {
            Configuration cfg = Configuration.defaultConfiguration();
            cfg.setPlaceholderStart(YamlConfigurerUtil.getStrYmlVal("beetl.delimiter_placeholder_start"));
            cfg.setPlaceholderEnd(YamlConfigurerUtil.getStrYmlVal("beetl.delimiter_placeholder_end"));
            cfg.setStatementStart(YamlConfigurerUtil.getStrYmlVal("beetl.delimiter_statement_start"));
            cfg.setStatementEnd(YamlConfigurerUtil.getStrYmlVal("beetl.delimiter_statement_end"));
            cfg.addPkg(YamlConfigurerUtil.getStrYmlVal("beetl.cfg_package"));
            GroupTemplate gt = new GroupTemplate(new ClasspathResourceLoader(), cfg);
            Template template = gt.getTemplate(readTlFileName);
            template.binding(context);
            String output = template.render();
            return output;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
